package org.newsexample.ws;

import org.newsexample.www.news.GetAllNews;

import org.newsexample.www.news.GetAllNewsResponse;
import org.newsexample.www.news.GetNewsByCategory;

import org.newsexample.www.news.GetNewsByCategoryResponse;
import org.newsexample.www.news.GetNewsByKeyword;

import org.newsexample.www.news.GetNewsByKeywordResponse;
import org.newsexample.www.news.News;
import org.newsexample.www.news.NewsArray;

import org.newsexample.www.news.GetNewsByCategoryFault;
import org.newsexample.www.news.GetNewsByKeywordFault;
import org.newsexample.www.news.GetAllNewsFault;

public class newsBusiness implements NewsSkeletonInterface
{

	@Override
	public GetNewsByKeywordResponse getNewsByKeyword(
			GetNewsByKeyword getNewsByKeyword) throws GetNewsByKeywordFault1 {
	
		GetNewsByKeywordResponse object = null;
		try
		{
		object = new GetNewsByKeywordResponse();
		
		String keyword = (String)getNewsByKeyword.getKeyword();
		
		NewsDAO dao = new NewsDAO();
		News[] newsResult = dao.getNewsByKeyword(keyword);
		
		NewsArray array = new NewsArray();
		array.setNews(newsResult);
		object.setGetNewsByKeywordResponse(array);

		}catch (ServiceUnavailableException e)
		{
			GetNewsByKeywordFault fault = new GetNewsByKeywordFault();
			fault.setGetNewsByKeywordFault(e.toString());
			GetNewsByKeywordFault1 error = new GetNewsByKeywordFault1();
			error.setFaultMessage(fault);
			throw error;
		}catch(BlankException e)
		{
			GetNewsByKeywordFault fault = new GetNewsByKeywordFault();
			fault.setGetNewsByKeywordFault(e.toString());
			GetNewsByKeywordFault1 error = new GetNewsByKeywordFault1();
			error.setFaultMessage(fault);
			throw error;
		}catch (Exception e)
		{
			GetNewsByKeywordFault fault = new GetNewsByKeywordFault();
			fault.setGetNewsByKeywordFault(ErrorMessages.ServiceUnavailableError);
			GetNewsByKeywordFault1 error = new GetNewsByKeywordFault1();
			error.setFaultMessage(fault);
			throw error;
		}
		return object;
	}

	@Override
	public GetNewsByCategoryResponse getNewsByCategory(
			GetNewsByCategory getNewsByCategory) throws GetNewsByCategoryFault1 {
		GetNewsByCategoryResponse object = null;
		try
		{
		object = new GetNewsByCategoryResponse();
		
		String category = (String)getNewsByCategory.getCategory();
		
		NewsDAO dao = new NewsDAO();
		News[] newsResult = dao.getNewsByCategory(category);
		
		NewsArray array = new NewsArray();
		array.setNews(newsResult);
		object.setGetNewsByCategoryResponse(array);
		
		}catch (ServiceUnavailableException e)
		{
			GetNewsByCategoryFault fault = new GetNewsByCategoryFault();
			fault.setGetNewsByCategoryFault(e.toString());
			GetNewsByCategoryFault1 error = new GetNewsByCategoryFault1();
			error.setFaultMessage(fault);
			throw error;
		}catch(BlankException e)
		{
			GetNewsByCategoryFault fault = new GetNewsByCategoryFault();
			fault.setGetNewsByCategoryFault(e.toString());
			GetNewsByCategoryFault1 error = new GetNewsByCategoryFault1();
			error.setFaultMessage(fault);
			throw error;
		}catch (Exception e)
		{
			GetNewsByCategoryFault fault = new GetNewsByCategoryFault();
			fault.setGetNewsByCategoryFault(ErrorMessages.ServiceUnavailableError);
			GetNewsByCategoryFault1 error = new GetNewsByCategoryFault1();
			error.setFaultMessage(fault);
			throw error;
		}
		return object;
	}

	@Override
	public GetAllNewsResponse getAllNews(GetAllNews getAllNews)
			throws GetAllNewsFault1 {
		try
		{
		GetAllNewsResponse object = new GetAllNewsResponse();
		
		NewsDAO dao = new NewsDAO();
		News[] newsResult = dao.getAllNews();
		
		NewsArray array = new NewsArray();
		array.setNews(newsResult);
		object.setGetAllNewsResponse(array);
		
		return object;
		}catch(ServiceUnavailableException e)
		{
			GetAllNewsFault fault = new GetAllNewsFault();
			fault.setGetAllNewsFault(e.toString());
			GetAllNewsFault1 error = new GetAllNewsFault1();
			error.setFaultMessage(fault);
			throw error;
			
		}catch(Exception e)
		{
			GetAllNewsFault fault = new GetAllNewsFault();
			fault.setGetAllNewsFault(ErrorMessages.ServiceUnavailableError);
			GetAllNewsFault1 error = new GetAllNewsFault1();
			error.setFaultMessage(fault);
			throw error;
		}
	}
}
